package com.ayoka.parenting.ui.meditation.detail;

import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.ViewGroup;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.BaseResponse;
import com.ayoka.parenting.bean.MeditationBean;
import com.ayoka.parenting.bean.MeditationDetailBean;
import com.ayoka.parenting.interf.SimpleRequestCallback;
import com.ayoka.parenting.mvp.BasePresenterImpl;
import com.jincp.common.dialog.DialogDismissListener;
import com.jincp.common.dialog.DialogResultListener;
import com.jincp.common.utils.UIUtils;

import java.util.Map;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: MeditationDetailPresenter.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class MeditationDetailPresenter extends BasePresenterImpl<MeditationDetailContract.View> implements MeditationDetailContract.Presenter{

    private MeditationDetailModel model;

     public MeditationDetailPresenter() {
        model = new MeditationDetailModel();
     }

    public void getMeditationDetailData(String meditationId) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.getMeditationDetailData(this, meditationId, new SimpleRequestCallback<MeditationDetailBean>() {
            @Override
            public void onSuccess(MeditationDetailBean data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().configMeditationDetailData(data);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showWaitDialog(errMsg);
            }
        });
    }

    @Override
    public void updateCompletedStatus(String recordId, int playingDuration) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.updateCompletedStatus(this, recordId, playingDuration, new SimpleRequestCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onUpdateCompletedStatusCallback(true);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showWaitDialog(errMsg);
                mView.get().onUpdateCompletedStatusCallback(false);
            }
        });
    }

    public void showMeditationSettingDialog(FragmentManager fragmentManager, MeditationDetailBean meditationDetailBean) {
        MeditationSettingDialog dialog = new MeditationSettingDialog.Builder()
                .setGravity(Gravity.BOTTOM)
                .setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .build();
        dialog.setMeditationDetailBean(meditationDetailBean);
        dialog.setDialogResultListener(new DialogResultListener() {
            @Override
            public void result(DialogFragment dialogFragment, Object result) {
                Map<String, Object> map = (Map<String, Object>) result;
                String operation = (String) map.get("Operation");
                if (TextUtils.equals("Audio", operation)){
                    if (!isAttached()) return;
                    mView.get().onSelectedMeditationAudio((MeditationBean.MeditationVoiceBean)map.get("Value"));
                } else if (TextUtils.equals("BackgroundMusic", operation)){
                    if (!isAttached()) return;
                    mView.get().onSelectedMeditationBackgroundMusic((MeditationDetailBean.BgMusicsBean)map.get("Value"));
                } else if (TextUtils.equals("BackgroundMusicVolume", operation)){
                    if (!isAttached()) return;
                    mView.get().onChangeMeditationBackgroundVolume((float)map.get("Value"));
                }
            }
        });
        dialog.setDialogDismissListener(new DialogDismissListener() {
            @Override
            public void dismiss(DialogFragment dialog) {
                if (!isAttached()) return;
                mView.get().onDialogDismiss(dialog);
            }
        });
        dialog.show(fragmentManager, "MeditationSettingDialog");
    }
}
